<?php
require_once __DIR__ . '/../vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;

// $exchange = 'pubSub'; // 交换器名称

try {
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); // 建立连接到RabbitMQ服务器
    $channel = $connection->channel(); // 建立通道
    $channel->queue_declare('liao', false, false, false, false); // 临时队列

    echo " [*] Waiting for messages. To exit press CTRL+C\n";

    $callback = function ($msg) {
        echo " 读取队列参数为： ", $msg->body, "\n";
    };

    // basic_qos方法设置参数prefetch_count = 1。这告诉RabbitMQ不要在一个时间给一个消费者多个消息（在处理和确认以前的消息之前，不要向消费者发送新消息。相反，它将发送给下一个仍然不忙的消费者）。
    // $channel->basic_qos(null, 1, null);
    $channel->basic_consume("liao", '', false, false, false, false, $callback);
    while (count($channel->callbacks)) { // 循环获取消息
        $channel->wait();
    }
    $channel->close();
    $connection->close();
} catch (Exception $e) {
    die($e->getMessage());
}